package listener;

import feasts.CalcFeasts;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import urlaub.Cal;
import urlaub.Cal2;
import urlaub.Urlaub;
import vars.Variables;

public class UrlaubListener implements ActionListener, Variables {

	private Cal left;
	private Cal2 right;
	private CalcFeasts cf;
	private ArrayList<String> urlaubDays = new ArrayList<String>();
	private Urlaub urlaub;
	private int[] beginDate;
	private int[] endDate;

	public UrlaubListener(Urlaub urlaub, Cal left, Cal2 right,
			CalcFeasts calcFeasts) {
		this.left = left;
		this.right = right;
		this.setCf(calcFeasts);
		this.urlaub = urlaub;
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		String command = e.getActionCommand();

		if (command.equals("Speichern")) {

			int dif = calcDiffDate(calcUrlaubEnd(), calcUrlaubBegin());

			Calendar urlaubCal = Calendar.getInstance();
			int day = calcUrlaubBegin()[0];
			int year = calcUrlaubBegin()[1];
			int monat = getBeginDate()[2];
			urlaubCal.set(Calendar.MONTH, monat);
			urlaubCal.set(Calendar.DAY_OF_MONTH, day);
			urlaubCal.set(Calendar.YEAR, year);

			String bId = urlaubCal.get(Calendar.DAY_OF_MONTH) + "A"
					+ urlaubCal.get(Calendar.YEAR) + "A"
					+ urlaubCal.get(Calendar.MONTH);

			this.urlaubDays.add(bId);
			this.urlaubDays.add("Urlaub");

			// generiere die Button-IDs in denen Urlaub ist
			for (int q = 0; q < dif; q++) {
				urlaubCal.add(Calendar.DAY_OF_YEAR, 1);
				this.urlaubDays.add(urlaubCal.get(Calendar.DAY_OF_MONTH) + "A"
						+ urlaubCal.get(Calendar.YEAR) + "A"
						+ urlaubCal.get(Calendar.MONTH));
				this.urlaubDays.add("Urlaub");
			}
			
			for (int i = 0; i < this.urlaubDays.size(); i++)
				getCf().setUrlaubIds(this.urlaubDays);
			getCf().markUrlaubsDays(this.urlaubDays, urlaubsColor);
			this.urlaub.dispose();
		}
	}

	/**
	 * Berechne die Differenz von zwei Kalendern in Tagen
	 * 
	 * @param string
	 *            End-Kalender
	 * @param string2
	 *            Start-Kalender
	 * @return Differenz in Tagen
	 */
	public int calcDiffDate(int[] end, int[] start) {
		// startdatum
		Calendar startCal = Calendar.getInstance();
		startCal.set(Calendar.DAY_OF_MONTH, start[0]);
		startCal.set(Calendar.MONTH, start[2]);
		startCal.set(Calendar.YEAR, start[1]);

		// enddatum
		Calendar endCal = Calendar.getInstance();
		endCal.set(Calendar.DAY_OF_MONTH, end[0]);
		endCal.set(Calendar.MONTH, end[2]);
		endCal.set(Calendar.YEAR, end[1]);

		int dif = endCal.get(Calendar.DAY_OF_YEAR)
				- startCal.get(Calendar.DAY_OF_YEAR);
		if (dif < 0)
			dif += startCal.getMaximum(Calendar.DAY_OF_YEAR);
		return dif;
	}

	private int[] calcUrlaubEnd() {
		String vonMonat = (String) right.getMonthChoice().getSelectedItem();
		int vonMonIndex = Arrays.asList(months).indexOf(vonMonat);
		int vonDayIndex = this.right.getDatum2();
		String vonYear = (String) right.getYearChoice().getSelectedItem();
		int vonYearIndex = new Integer(vonYear).intValue();
		int[] date = { vonDayIndex, vonYearIndex, vonMonIndex };
		setBeginDate(date);
		return date;
	}

	private int[] calcUrlaubBegin() {
		String vonMonat = (String) left.getMonthChoice().getSelectedItem();
		int vonMonIndex = Arrays.asList(months).indexOf(vonMonat);
		int vonDayIndex = this.left.getDatum1();
		String vonYear = (String) left.getYearChoice().getSelectedItem();
		int vonYearIndex = new Integer(vonYear).intValue();
		int[] date = { vonDayIndex, vonYearIndex, vonMonIndex };
		setEndDate(date);
		return date;
	}

	public void setCf(CalcFeasts cf) {
		this.cf = cf;
	}

	public CalcFeasts getCf() {
		return cf;
	}

	public ArrayList<String> getUrlaubDays() {
		return urlaubDays;
	}

	public void setUrlaubDays(ArrayList<String> urlaubDays) {
		this.urlaubDays = urlaubDays;
	}

	public void setBeginDate(int[] beginDate) {
		this.beginDate = beginDate;
	}

	public int[] getBeginDate() {
		return beginDate;
	}

	public void setEndDate(int[] endDate) {
		this.endDate = endDate;
	}

	public int[] getEndDate() {
		return endDate;
	}
	
	

	
}
